package vista;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.ArrayList;


import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;


import javax.swing.table.AbstractTableModel;


import util.Utilidades;

import modelo.Cliente;
import modelo.Status;
import modelo.dao.Servidor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgGestionarClientes extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTable tbClientes;
	private JScrollPane jScrollPane1;
	private JTextField txtCodigo;
	private JButton btBuscar;
	private JLabel lblSeleccionarCliente;
	private JButton btGestionarSucursal;
	private JButton btIncluir;
	private JButton btModificar;
	private JButton btEliminar;
	private JButton btVer;
	private JButton btSalir;
	private JLabel lblCodigo;
	private ArrayList<Cliente> clientes;
	private AbstractTableModel tbModelClientes;
	
	public DlgGestionarClientes() {
	}
	
	public DlgGestionarClientes(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				AnchorLayout thisLayout = new AnchorLayout();
				getContentPane().setLayout(thisLayout);
				this.setTitle("Gestionar Clientes");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1, new AnchorConstraint(240, 835, 677, 148, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					{
						clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);

						tbModelClientes = new AbstractTableModel() {
							@Override
							public Object getValueAt(int fil, int col) {
								switch (col) {
								case 0: return clientes.get(fil).getCodigo();
								case 1: return clientes.get(fil).getNombre();
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return clientes.size();
							}
							
							@Override
							public String getColumnName(int col) {
								switch (col) {
								case 0: return "Codigo";
								case 1: return "Nombre";
								}
								
								return null;
							}
							@Override
							public int getColumnCount() {
								return 2;
							}
						};
						tbClientes = new JTable();
						jScrollPane1.setViewportView(tbClientes);
						tbClientes.setModel(tbModelClientes);
						tbClientes.setBounds(66, 83, 352, 170);
					}
				}
				{
					lblCodigo = new JLabel();
					getContentPane().add(lblCodigo, new AnchorConstraint(80, 320, 113, 224, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					lblCodigo.setText("Codigo:");
					lblCodigo.setFont(new java.awt.Font("Tahoma",1,11));
					lblCodigo.setPreferredSize(new java.awt.Dimension(49, 14));
				}
				{
					txtCodigo = new JTextField();
					getContentPane().add(txtCodigo, new AnchorConstraint(64, 631, 129, 331, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					txtCodigo.setPreferredSize(new java.awt.Dimension(154, 28));
				}
				{
					btBuscar = new JButton();
					getContentPane().add(btBuscar, new AnchorConstraint(64, 744, 127, 664, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btBuscar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/buscar.PNG")));
					btBuscar.setPreferredSize(new java.awt.Dimension(41, 27));
				}
				{
					btSalir = new JButton();
					getContentPane().add(btSalir, new AnchorConstraint(877, 964, 970, 810, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setPreferredSize(new java.awt.Dimension(79, 40));
				}
				{
					btVer = new JButton();
					getContentPane().add(btVer, new AnchorConstraint(877, 777, 970, 623, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btVer.setText("Ver");
					btVer.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Ver.png")));
					btVer.setHorizontalTextPosition(JButton.CENTER);
					btVer.setVerticalTextPosition(JButton.BOTTOM);
					btVer.setPreferredSize(new java.awt.Dimension(79, 40));
				}
				{
					btEliminar = new JButton();
					getContentPane().add(btEliminar, new AnchorConstraint(877, 590, 970, 436, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btEliminar.setText("Eliminar");
					btEliminar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/eliminar.PNG")));
					btEliminar.setHorizontalTextPosition(JButton.CENTER);
					btEliminar.setVerticalTextPosition(JButton.BOTTOM);
					btEliminar.setPreferredSize(new java.awt.Dimension(79, 40));
				}
				{
					btModificar = new JButton();
					getContentPane().add(btModificar, new AnchorConstraint(877, 399, 970, 234, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btModificar.setText("Modificar");
					btModificar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Modificar.png")));
					btModificar.setHorizontalTextPosition(JButton.CENTER);
					btModificar.setVerticalTextPosition(JButton.BOTTOM);
					btModificar.setPreferredSize(new java.awt.Dimension(85, 40));
				}
				{
					btIncluir = new JButton();
					getContentPane().add(btIncluir, new AnchorConstraint(877, 199, 970, 45, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btIncluir.setText("Incluir");
					btIncluir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/incluir.PNG")));
					btIncluir.setHorizontalTextPosition(JButton.CENTER);
					btIncluir.setVerticalTextPosition(JButton.BOTTOM);
					btIncluir.setPreferredSize(new java.awt.Dimension(79, 40));
				}
				{
					btGestionarSucursal = new JButton();
					getContentPane().add(btGestionarSucursal, new AnchorConstraint(728, 950, 788, 622, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					btGestionarSucursal.setText("Gestionar Sucursales");
					btGestionarSucursal.setPreferredSize(new java.awt.Dimension(168, 26));
				}
				{
					lblSeleccionarCliente = new JLabel();
					getContentPane().add(lblSeleccionarCliente, new AnchorConstraint(142, 678, 234, 374, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					lblSeleccionarCliente.setText("Seleccionar Cliente:");
					lblSeleccionarCliente.setFont(new java.awt.Font("Tahoma",1,11));
					lblSeleccionarCliente.setPreferredSize(new java.awt.Dimension(156, 40));
				}
			}
			this.setSize(522, 462);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	DlgGestionarClientes dgcli=this;
	public void mostrar(JFrame frame) {
		
		setLocationRelativeTo(null);
		
		darFormatos();
		
		
		btBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				buscar();
			}
		});
		
		txtCodigo.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent ke) {
					if (ke.getKeyChar()== KeyEvent.VK_ENTER) {
						buscar();
					}
				}
			});

	
		
		btGestionarSucursal.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
			    
			   if(Utilidades.seleccion(tbClientes, dgcli, "Cliente"))
			   {	int i=dgcli.tbClientes.getSelectedRow();
			   		Cliente cli=clientes.get(i);
			   		DlgGestionarSucursal dgsuc= new DlgGestionarSucursal(dgcli,cli);
			   		dgsuc.mostrarGestionarSucursales(dgcli);
			   }
			    
			}
		});
		
		btIncluir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DlgCliente dcli= new DlgCliente(dgcli);
				   if(!Utilidades.campoVacio(dgcli, txtCodigo, "codigo"))
				   {	Cliente cli = Servidor.clientes().consultarClientePorCodigo(txtCodigo.getText());
						if(cli!=null)
						{	int i = clientes.indexOf(cli);
						  	tbClientes.setRowSelectionInterval(i, i);
						  	JOptionPane.showMessageDialog(dgcli, "EL cliente ya esta incluido","Valor existente",JOptionPane.ERROR_MESSAGE);
						}
						else
						 {	dcli.mostrarIncluir(dgcli,txtCodigo.getText());
							clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);
							tbModelClientes.fireTableDataChanged();
						 }
				   }
			}
		});
		
		btModificar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DlgCliente dcli= new DlgCliente(dgcli);
				   if(Utilidades.seleccion(tbClientes, dgcli, "Cliente"))
					{  
					   int i=dgcli.tbClientes.getSelectedRow();
					   Cliente cli=clientes.get(i);
					   dcli.mostrarModificar(dgcli, cli);
				      tbModelClientes.fireTableRowsUpdated(i, i);
				    }
			}
		});
		
		btEliminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				   if(Utilidades.seleccion(tbClientes, dgcli, "Cliente"))
					 {  
					   	int i=dgcli.tbClientes.getSelectedRow();
					    Cliente cli=clientes.get(i);
					   	int resp=JOptionPane.showConfirmDialog(dgcli, "Esta seguro de eliminar este registro: "+cli.getCodigo()+" ?", "Confirmar Eliminacion",JOptionPane.YES_NO_OPTION);
					   	if(resp==JOptionPane.YES_OPTION)
					   	{   cli.setStatus(cli.getStatus().INACTIVO);
					   		if(Servidor.clientes().modificarCliente(cli)!=null)
					   		{
					   			JOptionPane.showMessageDialog(dgcli, "Registro eliminado exitosamente","Eliminacion",JOptionPane.INFORMATION_MESSAGE);
					   			clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);
								tbModelClientes.fireTableDataChanged();
					   		}
					   		else
					   			JOptionPane.showMessageDialog(dgcli, "EL registro no pudo ser eliminado","Eliminacion",JOptionPane.ERROR_MESSAGE);
					   		
					   	}
					   	
					 }	
				  
			}
		});
		
		
		btVer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DlgCliente dcli= new DlgCliente(dgcli);
				   if(Utilidades.seleccion(tbClientes, dgcli, "Cliente"))
					{ Cliente cli=clientes.get(dgcli.tbClientes.getSelectedRow());
				      dcli.mostrarVer(dgcli, cli);
				    }
			}
		});
		
		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dgcli.dispose();
			}
		});
		
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		setVisible(true);
	}
	
	
	
	public void buscar()
	{   DlgCliente dcli  = new DlgCliente(dgcli);
		if(!(Utilidades.campoVacio(dgcli, txtCodigo, "Codigo")))
		{
			String cod = txtCodigo.getText(); 
			if (!cod.matches("[A-Z]-[0-9]{9,9}")) {
				JOptionPane.showMessageDialog(dgcli, "El codigo del campo no tiene el formato correcto, ejemplo: J-301123450");
			} else {
				Cliente c = Servidor.clientes().consultarClientePorCodigo(txtCodigo.getText());
				if(c!=null)
				{	int i = clientes.indexOf(c);
				  	tbClientes.setRowSelectionInterval(i, i);
				}
				else
				{	int resp=JOptionPane.showConfirmDialog(dgcli, "El Cliente no esta Registrado Desea Registrarlo?", "Confirmar Inclusion",JOptionPane.YES_NO_OPTION);
					if(resp==JOptionPane.YES_OPTION)
					{	dcli.mostrarIncluir(dgcli,txtCodigo.getText());
						clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);
						tbModelClientes.fireTableDataChanged();
					}
				}
			}
		}
	}
	public void darFormatos(){ 
		Utilidades.convertirMayuscula(txtCodigo);
	}

}
